package leetcode.problems;

import org.junit.Test;

import java.util.*;

/**
 * Created by gmwang on 2018/7/25
 * 两个句子中不常用的词
 */
public class _0801UncommonWordsFromTwoSentences {
    /**
     *
     * We are given two sentences A and B.  (A sentence is a string of space separated words.  Each word consists only of lowercase letters.)
     * 我们给出了两个句子A和B.（一个句子是一串空格分开的单词）。每个词仅由小写字母组成。
     *
     * A word is uncommon if it appears exactly once in one of the sentences, and does not appear in the other sentence.
     *
     * 如果一个词在句子中恰好出现一次，而不出现在另一个句子中，则是不常见的。
     * Return a list of all uncommon words.
     *
     * 返回所有不常用词的列表。
     *
     * You may return the list in any order.
     * 您可以按任何顺序返回列表。
     *
     * Example 1:
     *
     * Input: A = "this apple is sweet", B = "this apple is sour"
     * Output: ["sweet","sour"]
     * Example 2:
     *
     * Input: A = "apple apple", B = "banana"
     * Output: ["banana"]
     *
     *
     * Note:
     *
     * 0 <= A.length <= 200
     * 0 <= B.length <= 200
     * A and B both contain only spaces and lowercase letters.
     *
    /**
     * 获取每个句子中只出现过一次的词,再判断这个词在另一个句子中是否出现过
     * @param A,B
     * @return
     */
    public String[] uncommonFromSentences(String A, String B) {
        String[] As = A.split(" ");
        String[] Bs = B.split(" ");
        List<String> list = new ArrayList<>();
        Map<String, Integer> AMap = new HashMap<>();
        Map<String, Integer> BMap = new HashMap<>();
        for(int i=0;i< (As.length>=Bs.length?As.length:Bs.length);i++){
            if(i < As.length){
                //说明存在
                if(AMap.containsKey(As[i])) {
                    AMap.put(As[i],AMap.get(As[i]) + 1);
                }else{
                    AMap.put(As[i],1);
                }
            }
            if(i < Bs.length){
                //说明存在
                if(BMap.containsKey(Bs[i])) {
                    BMap.put(Bs[i],BMap.get(Bs[i]) + 1);
                }else{
                    BMap.put(Bs[i],1);
                }
            }
        }
        //取出只出现一次的，比较是否出现另一个map中
        for(int i=0;i< (As.length>=Bs.length?As.length:Bs.length);i++){
            if(i < As.length){
                //说明存在
                if(AMap.get(As[i]) != 1) {
                    //非 1 的什么也不干
                }else{
                    //不出现 则是不常见的
                    if(!BMap.containsKey(As[i])) list.add(As[i]);
                }
            }
            if(i < Bs.length){
                //说明存在
                if(BMap.get(Bs[i]) != 1) {
                    //非 1 的什么也不干
                }else{
                    //不出现 则是不常见的
                    if(!AMap.containsKey(Bs[i])) list.add(Bs[i]);
                }
            }
        }
        String[] array = new String[list.size()];
        for(int i = 0;i < list.size(); i ++){
            array[i] = list.get(i);
        }
        return array;
    }
    @Test
    public void test() {
        String sentence1 = "this apple is sweet";
        String sentence2 = "this apple is sour";
//        String sentence1 = "apple apple";
//        String sentence2 = "banana";
//        String sentence1 = "fd kss fd";
//        String sentence2 = "fd fd kss";
//        String sentence1 ="d b zu d t";
//        String sentence2 = "udb zu ap";
        String [] rs = uncommonFromSentences(sentence1,sentence2);
        System.out.println(Arrays.toString(rs));
    }
}
